package kbl.language;

import java.util.LinkedList;

public class DFAWalker implements DFAIterator<testDFAStateWrapper> {
		private DFAState root, current, next;
		private boolean finished = false;
		private LinkedList<DFAState> list = new LinkedList<DFAState>();

		public DFAWalker(DFAState dfa) {
			root = dfa;
			next = root;
			clearState();
		}
		
		@Override
		public void clearState(){
			finished = false;
			next = root;
			while (hasNext()) {
				current = next;
				current.marked = false;
				for (DFAState t : current.DFAMap.values()) {
					if (t.marked)
						list.addFirst(t);
				}
				if (list.isEmpty())
					finished = true;
				else
					next = list.removeFirst();				
			}
			finished = false;
			next = root;
		}

		@Override
		public boolean hasNext() {
			return !finished;
		}

		@Override
		public testDFAStateWrapper next() {
			current = next;
			current.marked = true;
			for (DFAState t : current.DFAMap.values()) {
				if (!t.marked)
					list.addFirst(t);
			}
			if (list.isEmpty())
				finished = true;
			else
				next = list.removeFirst();
			return new testDFAStateWrapper(current);
		}

		@Override
		public void remove() {
			throw new UnsupportedOperationException(
					"DFAWalker does not support remove()");
		}
}
